package com.ainge.easystudy.binary.tree;

import java.util.Stack;

/**
 * 最小栈
 *
 * @author: Ainge
 * @Time: 2021/9/16 23:16
 */
public class MinStack {

    private Stack<Integer> mainStack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();

    public void push(int element) {
        mainStack.push(element);
        if (minStack.empty() || element <= minStack.peek()) {
            minStack.push(element);
        }
    }

    public Integer pop() {
        if (mainStack.peek().equals(minStack.peek())) {
            minStack.pop();
        }
        return mainStack.pop();
    }

    public Integer getMin() {
        if (mainStack.empty()) {
            throw new IllegalArgumentException();
        }
        return minStack.peek();
    }


    public static void main(String[] args) {
        MinStack stack = new MinStack();
        stack.push(1);
        stack.push(4);
        stack.push(9);
        stack.push(7);
        stack.push(3);
        stack.push(8);
        stack.push(5);
        stack.pop();
        System.out.println(stack.getMin());
        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
        stack.pop();
        System.out.println(stack.getMin());

    }

}
